package com.jm.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jm.dao.AgentDao;
import com.jm.dao.ManagerDao;
import com.jm.dao.Utils;
import com.jm.viewmodel.ViewAgentProductPrice;
import com.jm.viewmodel.ViewArea;
import com.jm.viewmodel.ViewOrder;
import com.jm.viewmodel.ViewOrder.OrderItem;
import com.jm.viewmodel.ViewUserStock;
import com.opensymphony.xwork2.ActionSupport;

public class AgentAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6011057622866578932L;
	private Map<String, Object> pAgentsMap;
	private Map<String, Object> uStockMap;
	File orderPayImg;
	private Map<String, Object> addOrderMap;
	private Map<String, Object> editOrderMap;

	private Map<String, Object> getMyOrderMap;
	private Map<String, Object> getDownAgentsOrderMap;
	private Map<String, Object> editAreaMap;
	private Map<String, Object> getAreaMap;

	public Map<String, Object> getEditAreaMap() {
		return editAreaMap;
	}

	public void setEditAreaMap(Map<String, Object> editAreaMap) {
		this.editAreaMap = editAreaMap;
	}

	public Map<String, Object> getGetAreaMap() {
		return getAreaMap;
	}

	public void setGetAreaMap(Map<String, Object> getAreaMap) {
		this.getAreaMap = getAreaMap;
	}

	public Map<String, Object> getGetMyOrderMap() {
		return getMyOrderMap;
	}

	public void setGetMyOrderMap(Map<String, Object> getMyOrderMap) {
		this.getMyOrderMap = getMyOrderMap;
	}

	public Map<String, Object> getGetDownAgentsOrderMap() {
		return getDownAgentsOrderMap;
	}

	public void setGetDownAgentsOrderMap(Map<String, Object> getDownAgentsOrderMap) {
		this.getDownAgentsOrderMap = getDownAgentsOrderMap;
	}

	public Map<String, Object> getAddOrderMap() {
		return addOrderMap;
	}

	public void setAddOrderMap(Map<String, Object> addOrderMap) {
		this.addOrderMap = addOrderMap;
	}

	public Map<String, Object> getEditOrderMap() {
		return editOrderMap;
	}

	public void setEditOrderMap(Map<String, Object> editOrderMap) {
		this.editOrderMap = editOrderMap;
	}

	public File getOrderPayImg() {
		return orderPayImg;
	}

	public void setOrderPayImg(File orderPayImg) {
		this.orderPayImg = orderPayImg;
	}

	public Map<String, Object> getuStockMap() {
		return uStockMap;
	}

	public void setuStockMap(Map<String, Object> uStockMap) {
		this.uStockMap = uStockMap;
	}

	public Map<String, Object> getpAgentsMap() {
		return pAgentsMap;
	}

	public void setpAgentsMap(Map<String, Object> pAgentsMap) {
		this.pAgentsMap = pAgentsMap;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getAreas() {

		try {
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			getAreaMap = new HashMap<>();
			String uid = mRequest.getParameter("uid");
			long luid = Long.valueOf(uid);

			List<ViewArea> mAreas = new AgentDao().getAreaByUid(luid);
			if (mAreas != null && mAreas.size() > 0) {
				this.getAreaMap.put("result", mAreas);

			} else {
				this.getAreaMap.put("result", 0);
				this.getAreaMap.put("msg", "该用户没有区域信息");
			}

		} catch (Exception e) {
			// TODO: handle exception
			this.getAreaMap.put("result", "参数非法");
		}
		return SUCCESS;
	}

	public String addAreas() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			editAreaMap = new HashMap<>();
			String createuid = mRequest.getParameter("uid");
			String name = mRequest.getParameter("areaname");
			System.out.println(createuid + name);
			long cuid = Long.valueOf(createuid);

			int isAdd = new AgentDao().addArea(name, cuid);
			System.out.println(isAdd);
			if (isAdd == 0) {
				editAreaMap.put("result", true);
			} else if (isAdd == 2) {
				editAreaMap.put("result", false);
				editAreaMap.put("msg", "区域名已经存在");
			} else {
				editAreaMap.put("result", false);
			}

		} catch (Exception e) {
			// TODO: handle exception
			editAreaMap.put("result", "参数非法");
		}

		return SUCCESS;
	}

	public String getProductByAgentId() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = mRequest.getParameter("uid");
		pAgentsMap = new HashMap<>();

		if (Utils.isNumeric(uid)) {
			AgentDao mDao = new AgentDao();
			List<ViewAgentProductPrice> mAgentProductPrices = mDao.getProductByAgentId(Long.valueOf(uid));
			if (mAgentProductPrices != null && mAgentProductPrices.size() > 0) {
				for (ViewAgentProductPrice vapp : mAgentProductPrices) {
					pAgentsMap.put(String.valueOf(mAgentProductPrices.indexOf(vapp)), vapp);
				}
				this.setpAgentsMap(pAgentsMap);
				return SUCCESS;
			} else {
				pAgentsMap.put("msg", "没有获取到该用户产品信息");
				this.setpAgentsMap(pAgentsMap);
				return SUCCESS;
			}
		}else{
			pAgentsMap.put("msg", "参数非法");
			this.setpAgentsMap(pAgentsMap);
		}

		return SUCCESS;

	}

	public String getUserStocks() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = mRequest.getParameter("uid");
		uStockMap = new HashMap<>();
		if (uid == null) {
			uStockMap.put("msg", "参数不能为空");
			this.setuStockMap(uStockMap);
			return SUCCESS;
		}

		List<ViewUserStock> viewUserStocks = new AgentDao().getUserStockByUid(Long.valueOf(uid));
		if (viewUserStocks != null && viewUserStocks.size() > 0) {
			for (ViewUserStock viewUserStock : viewUserStocks) {
				uStockMap.put(String.valueOf(viewUserStocks.indexOf(viewUserStock)), viewUserStock);
			}
			this.setuStockMap(uStockMap);
		} else {
			uStockMap.put("msg", "没有查到库存信息");
			this.setuStockMap(uStockMap);
		}
		return SUCCESS;

	}

	public String getMyOrders() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = mRequest.getParameter("uid");
		String statue = mRequest.getParameter("statue");
		System.out.println(uid + statue);
		getMyOrderMap = new HashMap<>();
		if (uid == null) {
			getMyOrderMap.put("msg", "参数不能为空");
			this.setGetMyOrderMap(getMyOrderMap);
			return SUCCESS;
		}
		try {
			Long uidP = Long.valueOf(uid);
			int statueP = Integer.valueOf(statue);
			System.out.println("P " + uidP + statueP);
			List<ViewOrder> mOrders = new AgentDao().getOrdersByUserId(uidP, statueP);
			System.out.println(mOrders.size());
			if (mOrders != null && mOrders.size() > 0) {
				getMyOrderMap.put("result", mOrders);
				this.setGetMyOrderMap(getMyOrderMap);
				return SUCCESS;
			} else {
				getMyOrderMap.put("msg", "没有订单信息");
				getMyOrderMap.put("result", 0);
				this.setGetMyOrderMap(getMyOrderMap);
				return SUCCESS;
			}

		} catch (Exception e) {
			getMyOrderMap.put("msg", "参数非法");
			this.setGetMyOrderMap(getMyOrderMap);
			return SUCCESS;
		}

	}

	public String getDownOrders() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = mRequest.getParameter("uid");
		String statue = mRequest.getParameter("statue");
		getDownAgentsOrderMap = new HashMap<>();
		if (uid == null || statue == null) {
			getDownAgentsOrderMap.put("msg", "参数不能为空");
			this.setGetDownAgentsOrderMap(getDownAgentsOrderMap);
			return SUCCESS;
		}
		try {
			Long uidP = Long.valueOf(uid);
			int statueP = Integer.valueOf(statue);

			List<ViewOrder> mOrders = new AgentDao().getDownOrders(uidP, statueP);
			if (mOrders.size() > 0) {
				getDownAgentsOrderMap.put("result", mOrders);
				this.setGetMyOrderMap(getDownAgentsOrderMap);
				return SUCCESS;
			} else {
				getDownAgentsOrderMap.put("msg", "没有订单信息");
				getDownAgentsOrderMap.put("result", 0);
				this.setGetMyOrderMap(getDownAgentsOrderMap);
				return SUCCESS;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			getDownAgentsOrderMap.put("msg", "参数非法");
			this.setGetDownAgentsOrderMap(getDownAgentsOrderMap);
			return SUCCESS;
		}

	}

	public String addUserOrders() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = mRequest.getParameter("uid");
		String totalPrice = mRequest.getParameter("total");
		String orderItems = mRequest.getParameter("orderitems");
		addOrderMap = new HashMap<>();
		if (uid == null || orderItems == null) {
			addOrderMap.put("msg", "参数不能为空");
			this.setAddOrderMap(addOrderMap);
			return SUCCESS;
		}
		try {
			Long userid = Long.valueOf(uid);
			// for (String v : orderItemts1) {
			// System.out.println(v);
			// }
			ViewOrder viewOrder = new ViewOrder();
			String realPath = ServletActionContext.getServletContext().getRealPath("upload/pay");
			String picPath = "";
			/*
			 * if(orderPayImg!=null){ //去掉支付凭证图片上传 String picName =
			 * String.valueOf(System.currentTimeMillis()+".jpg"); picPath =
			 * "upload/pay/"+picName; File savefile = new File(new
			 * File(realPath),picName); try { FileUtils.copyFile(orderPayImg,
			 * savefile); viewOrder.setPayicon(picPath);
			 * 
			 * } catch (IOException | NumberFormatException e) { // TODO
			 * Auto-generated catch block e.printStackTrace();
			 * addOrderMap.put("result", false); addOrderMap.put("msg",
			 * "上传图片失败，请重新提交！"); return SUCCESS; } }else {
			 * addOrderMap.put("result", false); addOrderMap.put("msg",
			 * "上传图片失败，请重新提交！"); return SUCCESS; }
			 */
			Long vUid = Long.valueOf(uid);
			JSONArray orderItemJarry = JSON.parseArray(orderItems);

			List<OrderItem> orderItemsList = new ArrayList<>();
			for (Object object : orderItemJarry) {
				JSONObject jsonObject = (JSONObject) object;

				Long vPid = Long.valueOf(jsonObject.getString("pid"));
				String vName = jsonObject.getString("pname");
				String vPicUrl = jsonObject.getString("productpic");
				BigDecimal vPPrice = BigDecimal.valueOf(Double.valueOf(jsonObject.getString("productprice")));
				int vPcount = Integer.valueOf(jsonObject.getString("pcount"));
				// String vAddress = jsonObject.getString("address");
				// String vRemarks = jsonObject.getString("remarks");

				OrderItem orderItem = viewOrder.new OrderItem();
				// orderItem.setAddress(vAddress);
				orderItem.setPcount(vPcount);
				orderItem.setPid(vPid);
				orderItem.setPname(vName);
				orderItem.setProductprice(vPPrice);
				orderItem.setProductpic(vPicUrl);
				// orderItem.setRemarks(vRemarks);

				orderItemsList.add(orderItem);

			}
			viewOrder.setStatue(0);
			viewOrder.setUid(vUid);
			viewOrder.setTotalPrice(Integer.valueOf(totalPrice));
			viewOrder.setOrderItems(orderItemsList);
			Long result = new AgentDao().addUsrOrder(viewOrder);
			if (result != null && result > 0) {
				addOrderMap.put("result", true);
				this.setAddOrderMap(addOrderMap);
			} else {
				addOrderMap.put("result", false);
				this.setAddOrderMap(addOrderMap);
			}
		} catch (Exception e) {
			// TODO: handle exception
			addOrderMap.put("msg", "参数非法");
			this.setAddOrderMap(addOrderMap);
			return SUCCESS;
		}
		return SUCCESS;

	}

	public String confirmUserOrders() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String orderid = mRequest.getParameter("orderid");
		System.out.println(orderid);
		editOrderMap = new HashMap<>();
		if (orderid == null) {
			editOrderMap.put("msg", "参数不能为空");
			return SUCCESS;
		}

		try {
			Long orderidP = Long.valueOf(orderid);
			boolean result = new AgentDao().confirmOrder(orderidP);
			System.out.println("确认结果" + result);
			if (result) {
				editOrderMap.put("result", true);
				this.setEditOrderMap(editOrderMap);
				return SUCCESS;
			} else {
				editOrderMap.put("result", false);
				editOrderMap.put("msg", "可能库存不足，请查看库存");
				this.setEditOrderMap(editOrderMap);
				return SUCCESS;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			editOrderMap.put("msg", "参数非法");
			this.setEditOrderMap(editOrderMap);
			return SUCCESS;
		}

	}

}
